package com.lovo.action;

import com.google.gson.Gson;
import com.lovo.bean.Type;
import com.lovo.service.ITypeService;
import com.lovo.service.impl.TypeServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "TypeServlet",urlPatterns = "/type.do")
public class TypeServlet extends HttpServlet {

    private ITypeService its =new TypeServiceImpl();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        String method = request.getParameter("method");
        if(method==null||method.equals("")){
            showTypeList(request,response);
        }
    }
    protected void showTypeList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Type> typeList = its.getTypeList();
        String json = new Gson().toJson(typeList);
        System.out.println("json = " + json);
        response.getWriter().write(json);

    }
}
